//
//  facePageViewController.m
//  LT
//
//  Created by dllo on 16/5/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "facePageViewController.h"
#import "facePageTableViewCell.h"
#import "facePageCollectionViewCell.h"

#import "secondModel.h"
#import "ThirdModel.h"
#import "ZHNetworking.h"



static NSString *const FPCell = @"FPCell";
static NSString *const FPCollectionCell = @"FPCollectionCell";


@interface facePageViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UITableView *FPTableView;

@property(nonatomic,strong)UICollectionView *FPCollection;

@property(nonatomic,strong)NSMutableArray *myArray;

@property(nonatomic,strong)NSMutableArray *collectionArray;

//建立一个新的数组用来接收collectionView的值
@property(nonatomic,strong)NSMutableArray *faceArray;

//写缓存的数组
@property(nonatomic,strong)NSMutableArray *readArray;

@property(nonatomic,strong)NSMutableArray *groupArray;

@end

@implementation facePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self read];
    [self getData];
    
    //初始化数组
    self.myArray = [NSMutableArray array];
    self.collectionArray = [NSMutableArray array];
    self.faceArray = [NSMutableArray array];
    self.groupArray = [NSMutableArray array];

    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
        //建立tableView
    self.FPTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100* WIDTHCELL, (self.view.frame.size.height -112)* WIDTHCELL) style:UITableViewStylePlain];
    //  self.JinTableView.frame = CGRectMake(0, 0, 100, self.view.frame.size.height) ;
    self.FPTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.FPTableView];
    
    //取消垂直滑动标示符
    self.FPTableView.showsVerticalScrollIndicator = NO;
    self.FPTableView.delegate = self;
    self.FPTableView.dataSource = self;
  
    //注册tableViewCell;
    [self.FPTableView registerClass:[facePageTableViewCell class] forCellReuseIdentifier:@"FPCell"];
    
    
    //建立用来布局的flowLayout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake((self.view.frame.size.width -80), 120* WIDTHCELL);
   // flowLayout.minimumInteritemSpacing = 30;
    flowLayout.minimumLineSpacing = 10* WIDTHCELL;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(0* WIDTHCELL, 10* WIDTHCELL, 0* WIDTHCELL, 10* WIDTHCELL);
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.FPCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(100* WIDTHCELL, 0, (self.view.frame.size.width - 60)* WIDTHCELL, (self.view.frame.size.height - 64) * WIDTHCELL ) collectionViewLayout:flowLayout];
   // self.FPCollection.backgroundColor = [UIColor redColor];
    self.FPCollection.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFFFF,0XD3D3D3);
    
    [self.view addSubview:self.FPCollection];
    self.FPCollection.dataSource = self;
    self.FPCollection.delegate = self;
    
    //item注册
    [self.FPCollection registerClass:[facePageCollectionViewCell class] forCellWithReuseIdentifier:@"FPCollectionCell"];
    

    
}

//缓存读取
-(void)read{

    //取数据从本地plist
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *facePagePath = [path stringByAppendingPathComponent:@"facePage.plist"];
    NSArray *localFacePageArray = [NSArray arrayWithContentsOfFile:facePagePath];
    if (localFacePageArray.count != 0) {
        for (NSDictionary *nameDic in localFacePageArray) {
            secondModel *model = [[secondModel alloc]init];
            [model setValuesForKeysWithDictionary:nameDic];
            [self.myArray addObject:model];
            
            
        }

    }


}


//数据请求

-(void)getData{
    
    //GET请求
    [ZHNetworking zhGETWithURL:@"http://open.qyer.com/qyer/bbs/entry?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88283798007659&lon=121.539156053574&oauth_token=4ea07b0d41e13f5cb1448a45a3613e45&page=1&track_app_channel=App%2520Store&track_app_version=6.9.0&track_device_info=iPhone8%2C1&track_deviceid=F417850E-117E-4BF4-AAA6-3AD14E14E3C1&track_os=ios%25209.3.1&track_user_id=7891151&v=1" parameter:nil succes:^(id result) {
        
        NSDictionary *dataDic = [result objectForKey:@"data"];
        NSArray *forum_listArray = [dataDic objectForKey:@"forum_list"];
        
        //写入缓存
        //找到document路径
        NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
        NSLog(@"%@",path);//打印的目的是为了确定这个路径对不对
        //拼接一个自己的路径
        NSString *facePagePath = [path stringByAppendingPathComponent:@"facePage.plist"];
        //写入数据到对应路径的plist文件中
        [forum_listArray writeToFile:facePagePath atomically:YES];
        
        
        for (NSDictionary *nameDic in forum_listArray) {
            secondModel *model = [[secondModel alloc]init];
            [model setValuesForKeysWithDictionary:nameDic];
            [self.myArray addObject:model];

            
        }
        //刷新tableView
        [self.FPTableView reloadData];
        [self.FPTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
        
        //右边的collectionview也走第一个cell
        secondModel *type = [self.myArray objectAtIndex:0];
        
        [self.groupArray addObjectsFromArray:type.group];
        
    } failure:^(NSError *error) {
        
        
    }];
    
}




//===============tableView的俩种方法==========================
//cell的点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   //给model赋值
    secondModel *model = [self.myArray objectAtIndex:indexPath.row];
   //移除数组之前的值,因为每个都要更换新的
    [self.faceArray removeAllObjects];
    //给face数组赋值

    self.faceArray = [NSMutableArray arrayWithArray:model.group];
    //NSLog(@"1111111%@",self.faceArray);
    //刷新collectionView
    [self.FPCollection reloadData];
    
    
    
    
}

//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80 * WIDTHCELL;
    
}

//cell的行数(必备方法1)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  
    return self.myArray.count;
}

//必备方法2
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    facePageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FPCell forIndexPath:indexPath];
    secondModel *model = [self.myArray objectAtIndex:indexPath.row];
    
    cell.secondModel = model;

  
    return cell;
    
    
}

//====================collectionView===========
-(facePageCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
   //用faceArray给Model赋值
  ThirdModel *model = [self.faceArray objectAtIndex:indexPath.row];
    facePageCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"FPCollectionCell" forIndexPath:indexPath];
    
   item.thirdModel = model;
    //NSLog(@"%@",model.name);
    return item;
    
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
   // NSLog(@"22222%ld",self.faceArray.count);
    return self.faceArray.count;
}

//cell高度


//cell点击
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"数据维护!" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
